import Vue from 'vue'
import VueRouter from 'vue-router'

import Main from './views/Main.vue'

import Login from './views/Login.vue'

import CategoryEdit from './views/CategoryEdit'
import CategoryList from './views/CategoryList.vue'


import ItemEdit from './views/ItemEdit.vue'
import ItemList from './views/ItemList.vue'

import HeroEdit from './views/HeroEdit.vue'
import HeroList from './views/HeroList.vue'

import ArticleEdit from './views/ArticleEdit.vue'
import ArticleList from './views/ArticleList.vue'

import AdEdit from './views/AdEdit.vue'
import AdList from './views/AdList.vue'

import AdminUserEdit from './views/AdminUserEdit.vue'
import AdminUserList from './views/AdminUserList.vue'

Vue.use(VueRouter)

const routes = [
  {path:'/login',name:'login',component:Login,meta:{isPublic : true} }, //加meta，路由元信息，根据isPublic判断是否可用公开访问，public：公共，
  // 思路:通过判断是否有meta:{isPublic:true} （即是否有isPublic属性）来确定登录页，加上判断是否有localStorage.token（登录后服务端才返回token），m
  {
    path: '/',
    name: 'main',
    component: Main,
    children:[
      {path:'/categories/create',component:CategoryEdit},
      {path:'/categories/edit/:id',component:CategoryEdit,props:true},
      {path:'/categories/list',component:CategoryList},

      { path: '/items/create', component: ItemEdit },
      { path: '/items/edit/:id', component: ItemEdit, props: true },    //这里props：true，指ItemList将id值传递到ItemEdit组件中，看到ItemEdit组件接收id值
      { path: '/items/list', component: ItemList },

      { path: '/heroes/create', component: HeroEdit },
      { path: '/heroes/edit/:id', component: HeroEdit, props: true },
      { path: '/heroes/list', component: HeroList },

      { path: '/articles/create', component: ArticleEdit },
      { path: '/articles/edit/:id', component: ArticleEdit, props: true },
      { path: '/articles/list', component: ArticleList },

      { path: '/ads/create', component: AdEdit },
      { path: '/ads/edit/:id', component: AdEdit, props: true },
      { path: '/ads/list', component: AdList },

      { path: '/admin_users/create', component: AdminUserEdit },
      { path: '/admin_users/edit/:id', component: AdminUserEdit, props: true },
      { path: '/admin_users/list', component: AdminUserList },
    ]
  },
]

const router = new VueRouter({
  routes
})

// 实现效果：http://localhost;8080/#/+啥字符，都会跳转到login登录页
router.beforeEach((to,form,next)  => { //全局前置守卫beforeEach()，to：去到什么网页，from：什么网页，next：继续进行下面一系列操作（路由跳转等）
  // console.log(to.meta)    //路由任意跳转，除了login登录页都是{},只有login登录页{isPublic: true}
  if(!to.meta.isPublic && !localStorage.token) {    //没有public和token则要跳转到登录页
    // router.push('/login')    ??错的，必须要用next
    return next('/login')   //返回跳转到登录页
  }
  next()  //如果判断有isPublic和token，则继续进行操作，
  // next必须写，不然不能进行后面操作（作为中间件要连贯）
})


// next('/') 或者 next({ path: '/' }): 跳转到一个不同的地址。当前的导航被中断，然后进行一个新的导航。你可以向 next 传递任意位置对象，且允许设置诸如 replace: true、name: 'home' 之类的选项以及任何用在 router-link 的 to prop 或 router.push 中的选项。

// next(): 进行管道中的下一个钩子。如果全部钩子执行完了，则导航的状态就是 confirmed (确认的)。

export default router
